/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;



import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;


public class EditMember extends JFrame implements ActionListener
{
        // Variables declaration - do not modify
    private JPanel add;
    private JLabel addMember;
    //private JLabel bal;
    //private JLabel ballabel;
    private JButton cancel;
    private JLabel contactlabel;
    private JTextField contacts;
    private JLabel datejoinedlabel;
    private JLabel djoined;
    private JTextField firstname;
    private JLabel firstnamelabel;
  //  private JRadioButton male, female;
  //  private JLabel genderlabel;
    private JLabel header;
    private JLabel idlabel;
    private JTextField lastname;
    private JLabel lastnamelabel;
    private JLabel memID;
    private JSpinner memexp;
    private JLabel memexplabel;
    private JRadioButton member, notmember;
    private JLabel memstatlabel;
  //  private JTextField midinitial;
   // private JLabel midlabel;
    private JButton save;
    private JTextField addr;
    private JLabel addrlabel;
    private JOptionPane savemsg;
    private JOptionPane cancelmsg;
    private ButtonGroup memstat;
    public PreparedStatement pstmt;
    public Statement stmt1, stmt2;
    public ResultSet rset;
    public String query1, query2;
    public int n;
    public String ID;
    
    public EditMember()
    {
        super("Brickroad Fitness - Edit Member");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents(ID);
    }

    public EditMember(String ID)
    {
        super("Brickroad Fitness - Edit Member");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        this.ID=ID;
        System.out.println(ID);
        initComponents(ID);
    }

        private void initComponents(String ID) {

        header = new JLabel();
        add = new JPanel();
        addMember = new JLabel();
        idlabel = new JLabel();
        lastnamelabel = new JLabel();
        firstnamelabel = new JLabel();
     //   midlabel = new JLabel();
        addrlabel = new JLabel();
      //  genderlabel = new JLabel();
        memstatlabel = new JLabel();
        memexplabel = new JLabel();
        contactlabel = new JLabel();
        datejoinedlabel = new JLabel();
        //ballabel = new JLabel();
        memID = new JLabel();
        lastname = new JTextField();
        firstname = new JTextField();
      //  midinitial = new JTextField();
        addr = new JTextField();
     //   male = new JRadioButton();
     //   female = new JRadioButton();
        member = new JRadioButton();
        notmember = new JRadioButton();
        memexp = new JSpinner();
        contacts = new JTextField();
        djoined = new JLabel();
       // bal = new JLabel();
        save = new JButton();
        cancel = new JButton();
        savemsg = new JOptionPane();
        cancelmsg = new JOptionPane();
      //  gender = new ButtonGroup();
        memstat = new ButtonGroup();

      //  gender.add(male);
     //   gender.add(female);

        memstat.add(member);
        memstat.add(notmember);

        save.addActionListener(this);
        cancel.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        add.setBackground(new Color(255, 255, 102));
        add.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        addMember.setFont(new Font("Arial", 3, 18)); // NOI18N
        addMember.setText("Edit Member");

        idlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        idlabel.setText("Membership ID:");

        lastnamelabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        lastnamelabel.setText("Last Name:");

        firstnamelabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        firstnamelabel.setText("First Name:");

      //  midlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
      //  midlabel.setText("Middle Name:");

        addrlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        addrlabel.setText("Address:");

      //  genderlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
      //  genderlabel.setText("Gender:");

        memstatlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        memstatlabel.setText("Membership Status:");

        memexplabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        memexplabel.setText("Membership Expiration:");

        contactlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        contactlabel.setText("Contact Details:");

        datejoinedlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        datejoinedlabel.setText("Date Joined:");

       // ballabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
       // ballabel.setText("Balance:");

        memexp.setModel(new SpinnerDateModel());
        memexp.setEditor(new JSpinner.DateEditor(memexp, "yyyy-MM-dd"));

        memID.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //memID.setText("123456");

        lastname.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //lastname.setText("Lau");

        firstname.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //firstname.setText("Jamie");

      //  midinitial.setFont(new Font("Tahoma", 1, 12)); // NOI18N
      //  midinitial.setText("Ty");

        addr.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //addr.setText("221 New York Street Quezon City");

      /*  male.setBackground(new Color(255, 255, 102));
        male.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        male.setText("Male");

        female.setBackground(new Color(255, 255, 102));
        female.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        female.setSelected(true);
        female.setText("Female");*/

        member.setBackground(new Color(255, 255, 102));
        member.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        //member.setSelected(true);
        member.setText("Member");

        notmember.setBackground(new Color(255, 255, 102));
        notmember.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        notmember.setText("Not a member");

        contacts.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //contacts.setText("5466582");

        djoined.setFont(new Font("Tahoma", 1, 12)); // NOI18N
        //djoined.setText("13 August 2011");

        //bal.setFont(new Font("Tahoma", 1, 12)); // NOI18N
       // bal.setText("75 500");

        cnd a = new cnd();
        try{
                       a.connect();

                       System.out.println(ID);
                       stmt1 = a.con1.createStatement();
                       query1 = "SELECT * FROM `member` WHERE mID =" +ID;
                       rset = stmt1.executeQuery(query1);
                       rset.next();

                       memID.setText(ID);
                       lastname.setText(rset.getString("mLastname"));
                       firstname.setText(rset.getString("mFirstname"));
                       addr.setText(rset.getString("mAddress"));
                       contacts.setText(rset.getString("mContact"));
                       djoined.setText(rset.getString("mDateJoined"));
                       memexp.setValue(rset.getDate("mExpiration"));

                       if (rset.getString("mStatus").equals("Member"))
                           member.setSelected(true);
                       else
                           notmember.setSelected(true);

                       a.disconnect();
        }
        catch(Exception e){
            JOptionPane invalidsearchE = new JOptionPane();
            invalidsearchE.showMessageDialog(add, "ERROR: " +e.getMessage());
        }
        
        save.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        save.setText("SAVE");
        save.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cancel.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        cancel.setText("CANCEL");
        cancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        GroupLayout addLayout = new GroupLayout(add);
        add.setLayout(addLayout);
        addLayout.setHorizontalGroup(
            addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(addLayout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(idlabel)
                    .addComponent(lastnamelabel)
                    .addComponent(firstnamelabel)
                  //  .addComponent(midlabel)
                    .addComponent(addrlabel)
                   // .addComponent(genderlabel)
                    .addComponent(memstatlabel)
                    .addComponent(memexplabel)
                    .addComponent(contactlabel)
                    .addComponent(datejoinedlabel))
                   // .addComponent(ballabel))
                .addGap(29, 29, 29)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(memID, GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                    .addComponent(lastname)
                    .addComponent(firstname)
                   // .addComponent(midinitial, GroupLayout.Alignment.TRAILING)
                    .addComponent(addr)
                    .addComponent(memexp)
                    .addComponent(contacts)
                    .addComponent(djoined)
                    //.addComponent(bal)
                .addGroup(addLayout.createSequentialGroup()
                     .addGroup(addLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         // .addComponent(male)
                          .addComponent(member))
                     .addGap(46, 46, 46)
                     .addGroup(addLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addComponent(notmember))))
                          //.addComponent(female))))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(cancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(save, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addGap(32, 32, 32))
            .addGroup(addLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(addMember)
                .addContainerGap(767, Short.MAX_VALUE))
        );
        addLayout.setVerticalGroup(
            addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(addLayout.createSequentialGroup()
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(addLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(addMember)
                        .addGap(18, 18, 18)
                        .addComponent(idlabel))
                    .addGroup(addLayout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(memID, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(lastnamelabel)
                    .addComponent(lastname, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(firstnamelabel)
                    .addComponent(firstname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                /*.addGap(7, 7, 7)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(midlabel)
                    .addComponent(midinitial, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))*/
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(addrlabel)
                    .addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
               /* .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(genderlabel)
                    .addComponent(male)
                    .addComponent(female))*/
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(memstatlabel)
                    .addComponent(member)
                    .addComponent(notmember))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(addLayout.createSequentialGroup()
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(memexplabel)
                            .addComponent(memexp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(contactlabel)
                            .addComponent(contacts, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(datejoinedlabel)
                            .addComponent(djoined, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        /*.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(ballabel)
                            .addComponent(bal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))*/)
                    .addGroup(addLayout.createSequentialGroup()
                        .addComponent(save, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cancel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(header, GroupLayout.PREFERRED_SIZE, 919, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(add, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(add, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();

        if (event == save)
        {
            if(!(memID.getText().isEmpty()) && !(lastname.getText().isEmpty())
                    && !(firstname.getText().isEmpty())
                    && !(addr.getText().isEmpty())
                    && !(memstat.getSelection() == null)
                    && !(contacts.getText().isEmpty()))
           {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                int ans;
                ans = savemsg.showConfirmDialog(this, "Do you really want to save the changes you've done?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (ans == 0)
                {
                    try{
                        cnd a = new cnd();
                        a.connect();
                        stmt2 = a.con2.createStatement();                  
                        query2 = "UPDATE `member`" +
                        "SET `mFirstname` = \"" + firstname.getText() + "\", `mLastname` = \"" + lastname.getText() + "\", `mAddress` = \"" + addr.getText() + "\", `mContact` = \"" + contacts.getText()+
                        "\", `mExpiration` = \"" + sdf.format(memexp.getValue()) + "\" WHERE `mID` = " + ID;
                            stmt2.executeUpdate(query2);

                         if (member.isSelected())
                         {
                             stmt2 = a.con2.createStatement();
                             query2 = "UPDATE `member`" +
                             "SET `mStatus` = \"" + "Member" + "\" WHERE `mID` = " + ID;
                             stmt2.executeUpdate(query2);
                         }
                         else
                         {
                             stmt2 = a.con2.createStatement();
                             query2 = "UPDATE `member`" +
                             "SET `mStatus` = \"" + "Not a Member" + "\" WHERE `mID` = " + ID;
                             stmt2.executeUpdate(query2);
                         }
                         savemsg.showMessageDialog(this, "Save successful!", "Save", JOptionPane.INFORMATION_MESSAGE);
                         System.out.println("Edit Successful");
                         dispose();
                         System.out.println(ID);
                         new MemberProfile(ID);
                         a.disconnect();
                     }
                    catch(Exception z){
                             JOptionPane invalidsearchE = new JOptionPane();
                             invalidsearchE.showMessageDialog(add, "ERROR: " +z.getMessage());
                             System.out.println("Error copying records from member table. " + z.getMessage());
                    }
                }
            }
            else
                savemsg.showMessageDialog(this, "Incomplete fields", "Error", JOptionPane.ERROR_MESSAGE);
        }

        if (event == cancel)
        {
            int ans;
            ans = cancelmsg.showConfirmDialog(this, "Are you sure you want to cancel? All unsaved information will be lost.", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(ans == 0)
            {
                dispose();
                new MemberProfile(ID);
            }
        }
    }
}